import * as PIXI from "@tbminiapp/pixi-miniprogram-engine";
import { Sprite,Texture,Text,Container,Graphics,BLEND_MODES } from "@tbminiapp/pixi-miniprogram-engine";

/**
 * 纹理创建动画
 * @param { array } frames 动画纹理
 * @param { object } speed 播放速度
 * @param { number } scale 缩放 
 * @param { boolean } loop 是否循环
 * @param { number } callback 播放结束回调
 */
exports.createAnimate = function(frames,speed=1,loop=true,scale=2,callback) {
  let frameArray = []
  for(var i in frames){
    var texture = new Texture.from(frames[i]);
    frameArray.push(texture);  
  }

  var aniSprite = new PIXI.extras.AnimatedSprite(frameArray);
  aniSprite.scale.set(scale);
  aniSprite.loop = loop;
  aniSprite.animationSpeed = speed;
  
  // aniSprite.onFrameChange = function() {};
  if(callback)
    aniSprite.onComplete = ()=>{ callback();};

  return aniSprite;
},
/**
 * @param { string } url 雪碧图链接
 * @param { object } aniCfg 雪碧图配置
 * @param { number } speed 播放速度
 * @param {number} scale 缩放
 * @param { boolean } loop 是否循环
 * @param { function } callback 回调函数
 */
exports.createAnimateFromAtlas = function(url,aniCfg,aniName,speed = 1,loop=true,scale=1,onFrameChange,onComplete) {
  let frameArray = []
  var mainTexture = Texture.fromImage(url);
  let { animations,frames } = aniCfg;
  let anis = animations[aniName];
  if(!anis ) return false;
  for(var i=0;i<anis.length;i++){
    let frame = frames[anis[i]].frame;
    var rectangle = new PIXI.Rectangle(frame.x, frame.y, frame.w, frame.h);
    var texture = new Texture(mainTexture, rectangle);
    frameArray.push(texture);  
  }
  
  var aniSprite = new PIXI.extras.AnimatedSprite(frameArray);
  aniSprite.scale.set(scale);
  aniSprite.loop = loop;
  aniSprite.animationSpeed = speed;
  
  if(onFrameChange)
    aniSprite.onFrameChange = function() { onFrameChange();};
  if(onComplete)
    aniSprite.onComplete = ()=>{ onComplete();};

  return aniSprite;
},

/**
 * 创建文本
 * @param { string } text 文本内容
 * @param { object } style 文本样式
 * @param { object } fontFamily 字体  'Arial'
 * @param { string } style.fill 文字颜色 #fff
 * @param { string } style.stroke 描边 #000
 * @param { number } style.strokeThickness 描边宽
 * @param { number } style.fontSize 字号 30
 * @param { string } style.align 对齐方式 center
 */
exports.createText = function(text,style = {fontFamily:Arial,fill:'#fff',stroke:'#000',strokeThickness:0,fontSize:30,align:"center"}){
  var textSpr = new Text(text,style);
  return textSpr;
},
/**
 * 创建显示对象
 * @param  {[type]} url   资源路径
 * @param  {[type]} scale 缩放比例
 * @return {[type]}       [description]
 */
exports.createSprite = function(url, scale = 1) {
  let sprite = new Sprite(
    Texture.from(url)
  );
  sprite.scale.set(scale);
  return sprite;
},
/**
 * 纹理集创建显示对象
 * @param { string } url 雪碧图
 * @param { object } config 配置信息
 * @param { object } scale 缩放
 * @param { number } scale.x 缩放
 * @param { number } scale.y 缩放
 */
exports.getTextureFromAtlas = function(url,config) {
  var mainTexture = Texture.fromImage(url);
  var rectangle = new PIXI.Rectangle(config.x, config.y, config.w, config.h);
  var texture = new Texture(mainTexture, rectangle);
  return texture;
}